<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Request;
use app\admin\model\NewsClass as NewsClassModel;
use app\admin\model\News as NewsModel;
use think\Db;
class News extends Base
{
	// 文章列表
    public function index()
    {
        $list = Db::table(array('news','news_class'))
        	->where("news.news_class=news_class.id and news.is_del=1")
        	->field('news.id as id, news.news_class as news_class, news_class.name as news_class_name,news.title as title,news.desc as desci,news.create_time as create_time,news.keywords as keywords,news.view_times as view_times')
        	->order('news.id desc')->paginate(14);
        $total = $list->total();
        $page = $list->render();
        $this -> view -> assign('total', $total);
        $this -> view -> assign('list', $list);
        $this -> view -> assign('page', $page);
    	return $this -> view-> fetch('news/news_list');
    }
	// 查询已删除的文章
    public function is_del()
    {
        $list = Db::table('news')->where("is_del = 2")->order('id desc')->paginate(10);
        $total = $list->total();
        $page = $list->render();
        $this -> view -> assign('total', $total);
        $this -> view -> assign('list', $list);
        $this -> view -> assign('page', $page);
    	return $this -> view-> fetch('news/news_del');
    }
    // 上传图片
    public function upload(Request $request)
    {
         $file = request()->file('file');
    
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $path = DS.'uploads'.DS.$info->getSaveName();
				$pathRes = str_replace("\\","/",$path);
                return $pathRes;
            }else{
                // 上传失败获取错误信息
                return $file->getError();
            }
        }   
    }
    // 添加文章页面
    public function add_news()
    {
        $newsType = Db::table('news_class')->where("is_del = 1")->order('id desc')->select();
        $this -> view -> assign('newsType', $newsType);
        return $this -> view-> fetch('news_add');
    }
    // 查看一篇文章
    public function preview () {
        $param = input('param.');
        $id = $param['id'];
		Db::table('news')->where('id', $id)->setInc('view_times', 1);
        $data = Db::table('news')->where('id',$id)->find();
        $this -> assign('data', $data);
		$this -> assign('current', 'news');
        return $this -> fetch('news_preview');
    }
    // 添加一篇文章
    public function create_news(Request $request)
    {
        $param = $request->param();
        $file = request()->file('zoom_img');
        if($file){
            $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
            if($info){
                $imgName = $info->getSaveName();
                $path = DS.'uploads'.DS.$info->getSaveName();
				$pathRes = str_replace("\\","/",$path);
                $param['zoom_img'] = $pathRes;
            }
        }
        $res = Db::table('news')->insert($param);
		if ($res) {
			return ['success'=> true, 'msg'=>'创建成功'];
		} else {
			return ['success'=> false, 'msg'=>'创建失败'];
		}
    }
	// 更改一篇文章
    public function update_news(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
		
        $file = request()->file('zoom_img');
        if($file){
            $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
            if($info){
                $imgName = $info->getSaveName();
                $path = DS.'uploads'.DS.$info->getSaveName();
				$pathRes = str_replace("\\","/",$path);
                $param['zoom_img'] = $pathRes;
            }
        }
		$res = Db::table('news')->update($param);
		if ($res) {
			return ['success'=> true, 'msg'=>'创建成功'];
		} else {
			return ['success'=> false, 'msg'=>'创建失败'];
		}
    }
    // 删除文章
    public function delete_news()
    {
        $param = input('param.');
        $id = $param['id'];
		$res = Db::table('news')->where('id',$id)->update(['is_del' => 2]);//1表示未删除 2表示已删除
		if ($res) {
			return ['success'=> true, 'msg'=>'删除成功'];
		} else {
			return ['success'=> false, 'msg'=>'删除失败'];
		}
    }
	// 编辑文章
    public function edit_news()
    {
        $param = input('param.');
        $id = $param['id'];
		$data = Db::table('news')->where('id',$id)->find();
        $this -> assign('data', $data);
		$newsType = Db::table('news_class')->where("is_del = 1")->order('id desc')->select();
        $this -> assign('newsType', $newsType);
		return $this -> fetch('news_edit');
    }
	// 彻底删除文章
    public function clear_news()
    {
        $param = input('param.');
        $id = $param['id'];
		$res = Db::table('news')->where('id',$id)->delete();
		if ($res) {
			return ['success'=> true, 'msg'=>'删除成功'];
		} else {
			return ['success'=> false, 'msg'=>'删除失败'];
		}
    }
}
